<template>
  <a-modal
    title="编辑"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['id', { initialValue: model?.id }]" disabled />
        </a-form-item>
        <a-form-item label="经销商">
          <a-select v-decorator="['supplyId']" placeholder="请选择经销商">
            <a-select-option :value="i.id" v-for="i in this.supplyoption" :key="i.id">{{ i.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="销售业务">
          <a-select v-decorator="['empId']" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in this.useroption" :key="i.id">{{ i.userName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="出库日期">
          <a-date-picker v-decorator="['outDate']" valueFormat="YYYY-MM-DD"/>
        </a-form-item>
        <a-form-item label="备注">
          <a-input v-decorator="['memo']" placeholder="请输入备注" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getEmployeesListUsingPOST } from '@/api/api/yuangongguanli.ts'
import { getSuppliesListUsingPOST } from '@/api/api/jingxiaoshangyunyingshangguanli.ts'
import pick from 'lodash.pick'
// 表单字段
const fields = ['supplyId','empId', 'outDate', 'memo',  'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
      optionParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
    }
    return {
      useroption: [],
      supplyoption: [],
      form: this.$form.createForm(this),
    }
  },
  mounted() {
    this.getSupOption()
    this.getUserOption()
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    getSupOption() {
      getSuppliesListUsingPOST({ ...this.optionParam, types:'2,3' })
        .then((res) => {
          this.supplyoption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    getUserOption() {
      getEmployeesListUsingPOST(this.optionParam)
        .then((res) => {
          this.useroption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
}
</script>
